package com.Akronmcp.hta.obj;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.bukkit.Bukkit;
import org.bukkit.boss.BarColor;
import org.bukkit.boss.BarFlag;
import org.bukkit.boss.BarStyle;
import org.bukkit.boss.BossBar;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;

import com.Akronmcp.hta.main.HM;

import me.clip.placeholderapi.PlaceholderAPI;

public class HopBar extends HPar2{

	
	public BossBar bar ;
    double pro[] ;

	String barhop = "BYST";
	
	
	public HopBar(HManager hta)
	{
		super(hta);
		bar = Bukkit.createBossBar("", BarColor.BLUE,BarStyle.SOLID);
		
	    text = "";
	}
	
	public HopBar() 
	{
		
		bar = Bukkit.createBossBar("", BarColor.BLUE,BarStyle.SOLID);
		
	    text = "";
	}

	/**设置血条参数*/
	public void set(String title,String bc,String bs,String bf)
	{
	   
		this.setText(title);
		bar.setColor(BarColor.valueOf(bc));
		bar.setStyle(BarStyle.valueOf(bs));
		
		if(bf!=null)
		bar.addFlag(BarFlag.valueOf(bf));
	}	
	
	
	
	/**获取血条实例*/
	public BossBar getBar()
	{
		return bar;
	}
	
	/**设置血条的特效*/
	public void setBarHop(String type) 
	{
		barhop = type;
	}
	
	public String getBarHop() 
	{
		return barhop;
	}

	/**发送给所有玩家*/
    public void sendToAll()
    {
    	Collection<? extends Player> pl= Bukkit.getOnlinePlayers();
    	//获取在线玩家
        if(!pl.isEmpty())
        send(new ArrayList<Player>(pl));
       
    }
	
    /**发送给玩家*/
	public void send(List<Player> pl) {
		
		if(pl==null||pl.isEmpty()) 
		return;
		
		if(module!=null)
		if(!iscopyedmodule||module.equals("auto"))
		this.setModule(module); 
		
		
			
		 Map<String,Parg> ppa = new HashMap<String,Parg>();
		 List<String> sl2 = new ArrayList<String>();	
		 
		 for(Player p2:pl)
 		 {	//每个玩家分配一个list等对应参数
			
		     Player pp = p2;
			 //pp=需解析papi的对象
			 
 		   if(papi)//解析papi
		   {
 		     
 			 String ttext = PlaceholderAPI.setPlaceholders(pp,text);
 		     sl2 = new ArrayList<String>();	
 			   int j = 0;
 			   //遍历index
 		   
 		 if(!gethoptypes().isEmpty()) 
 		 for(String type:gethoptypes())
 		 {//遍历闪烁种类s
 			 
 		   String[] thta2 =  PlaceholderAPI.setPlaceholders(pp,
 				   Arrays.asList(getHopargs().get(j))).toArray(new String[0]);
 		   //解析所有参数
 		   j++;
 		   
 		   
 		       try {
 		    	   
			  HDancer.Dance(sl2,type,(int) ticktime,
					   ttext,thta2);
			
		        }catch (Exception e) 
 		        {e.printStackTrace();}
		        //生成特效list
 		       
 		 }
 		   
 		  
 		}else
 		{//无papi则默认
 			
 			sl2.addAll(sl);
 		}
 		   
 		BossBar bar2 = null;  
 		
		bar2 = Bukkit.createBossBar("",
				bar.getColor(),
				bar.getStyle());	
		
		bar2.addPlayer(p2);
 		
 		  
 		  Parg pa1 = new Parg();//每个玩家对应的list等参数
 		  
 		  pa1.tbar = bar2;
 		  
 		  pa1.tcycle = getCycle();
 		  pa1.index = 0;
 		  pa1.tsl = sl2;
 		  if(endbyhop)
 		  {
 			  pa1.times = sl2.size()*getCycle();
 			  pa1.ttimes = pa1.times;
 			  pa1.sectime = (int)((pa1.times*ticktime)/20);
 		  }
 		  
 		  
 		  ppa.put(p2.getName(),pa1); 
 		  
	}//for
		 
		 

		new BukkitRunnable() {
			
			//_都是临时变量
			boolean isend = false;
			//是否已循环end
			float time = getStayTime();
            //总时间
            int tcycle = getCycle();
            //循环次数
            int index = 0;
            //下标
            float ticktime = getTickTime();
            //闪动间隔
            float times = time/ticktime;//((20/(double)ticktime))*time; 
            //闪动总次数
            float ttimes = times;
            //闪动剩余次数
            int secTime = (int)(time/20);
            
            List<String> tsl;
            BossBar tbar;
            		
            int size;
            
            public void run() 
            {//重写run方法
       
                if (ttimes > 0) 
                {//计时条件
      
                	
                	
                    for(Player p3:pl)
                    {//遍历玩家
                    	  String name2 = p3.getName();
                    	  Parg pa = ppa.get(name2);
                    	
                    	  tbar = pa.tbar;
                    	  
                          
                   if(endbyhop)  	 
                  {   	 
                    	  float tttimes = pa.ttimes;
                    	  
                    	  if(tttimes < 1)
                    	  {//次数为0则跳过
                    		 
                    		  if(!tbar.getPlayers().isEmpty())
                    		  tbar.removeAll();
                    		  
                    		  continue;
                    	  }else
                    	  {
                    		  ttimes = tttimes;
                    		  
                    	  }
                    	  secTime = pa.sectime;
                    	  times = pa.times;
                 }   	  
                    	  
                    	  isend = pa.isend;
                    	  
                    	  if(!isend)
                    	  {//结束循环前有意义
                    		  
                    	  tsl = pa.tsl;
                    	  tcycle = pa.tcycle;
                    	  index = pa.index;
                    	  
                    	  size = tsl.size();  
                    	  }   	  
                    	  
                      
                      
                      if(!isend)
                	  if((!tsl.isEmpty())&&tcycle > 0)
                      {//开始闪烁,遍历集合
  	  
                	  String str = tsl.get(index);
//                	  str = str.replace("&t", String.valueOf((int)ttimes));
//	               	  str = str.replace("&T", String.valueOf((int)(times-ttimes)));
	               	  str =  str.replace("&s", String.valueOf((int)((ttimes/20)*ticktime)));
	               	  str =  str.replace("&S", String.valueOf(secTime - (int)((ttimes/20)*ticktime)));  	 
               	      
                	  if(str!=null)                	 
                	  if(!str.equals("-"))
                      {
                		  tbar.setTitle(str);
                      }
                		  //设置文本
   	  
                     }else//如果集合空或停止循环
                     {
                    	 
                    	if(!pa.isend2)
                    	{//如果未使用默认文本
                    		
	                    	if(papi)
	                    	{//特效结束后用默认文本
	                    	  tbar.setTitle(PlaceholderAPI.setPlaceholders(p3, text)); 	                		 
	                    	  
	                    	}else
	                    	{
	                			tbar.setTitle(text);
	                    	}
	                    	
	                    	pa.isend2=true;
	                    	//已使用默认文本
                    	}
                    	

                    }
                    
                    //_设置bar
               
                    	switch(barhop)
                    	{
                    	case "BYTT":
                    		tbar.setProgress((double)(ttimes)/times);
                    		break;
                    		
                    	case "BYTT_OB":
                    		tbar.setProgress(1.0-(double)((ttimes)/times));
                    		break;
                    		
                    	case "BYST":
                    		tbar.setProgress((double)((int)((ttimes)/(20/(double)ticktime)))/secTime);
                    		break;
                    		
                    	case "BYST_OB":
                    		tbar.setProgress(1.0-(double)((int)((ttimes)/(20/(double)ticktime)))/secTime);
                    		break;
                    		
                    	default:
                    		tbar.setProgress(1.0);
                    		
                    	}
                    
                  
                     if(!isend)
                     {///
                     if(tcycle>=1)     	
                     {//
                	   if(index+1 < size)
                       {
                   	    index++;
                   	  
                       }else 
                       {//循环一次后
                   	    index = 0;
                   	    tcycle--;

                       }
                     }else {isend = true;} //
                    	
                    	pa.tcycle = tcycle;
                   		pa.index = index;
                   		pa.isend = isend;  
                   		  
                    	
                     }///
                          		 
                   	  
                     if(endbyhop)
                 	  {
                 		                    	          	  
                 	  pa.ttimes = ttimes -1;
                 	  
                 	  ttimes = 2;
                 	  //还原ttimes,让线程继续,说明还有其他玩家没有end
                 	  //当所有玩家都ttimes<1,则最后一次for遍历不会执行到此处
                 	  //直接导致ttimes被减少成0
                 	  }
                   	  
                    }
                	
                	
                    ttimes--;
                    
                }
                else 
                {//时间到或不在线
                     
                   if(!endbyhop)
                   for(Player p4:pl)
                   {//清除bar
                
                		tbar = ppa.get(p4.getName()).tbar;
                		tbar.removeAll();        	
                   }
                    cancel();//终止
                }
            } 
        }.runTaskTimerAsynchronously(HM.getPlugin(HM.class), 0L, ticktime);	   
		
        
        sendSubtext(pl);
	   	 //伴随文本
	}
	
	public void addToList(String id)
	{
		this.id = id;
		
		hta.addBar(this);
		
	}
	public void addToList()
	{
		if(id!=null)
		hta.addBar(this);
	}
	class Parg
	{
	   List<String> tsl;
	   int index = 0;
	   int tcycle = 1;
	   
	   float ttimes;
	   float times;
	   int sectime;
	   
	   boolean isend =false;
	   //是否特效已结束
	   
	   boolean isend2 =false;
	   //是否已使用默认文本
	   
	   BossBar tbar;
	}
	
	/**直接通过参数生成并发送一个临时血条*/
	public static void sendBy(Player p,String text,int time)
	{
	   
	   HopBar hb = new HopBar();
	   hb.setStayTime(time);
	   hb.setText(text);
	   
	   hb.send(p);
	}
	
	/**直接通过参数生成并发送一个临时血条*/
	public static void sendBy(Player p,String text,String module,boolean ispapi)
	{
       sendBy(Arrays.asList(p),text,module,ispapi);
	}

	/**直接通过参数生成并发送一个临时血条*/
	public static void sendBy(List<Player> pl,String text,String module,boolean ispapi)
	{
		HopBar hb = new HopBar();
		hb.setPapi(ispapi);
		hb.setText(text);
		hb.setModule(module);
	    
		 for(Player p:pl)
		  hb.send(p);
	}

	/**直接通过参数生成并发送给所有玩家一个临时血条*/
	public static void sendtoallBy(String text,String module,boolean ispapi)
	{
		sendBy(new ArrayList<Player>(Bukkit.getOnlinePlayers()),text,module,ispapi);
	}
}

  
